<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('标准仓库列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse" >
                <div class="form-group" style="float: right;">
                    <label class="checkbox-inline check-box">
                        <input type="text" class="form-control" style="width: 230px" placeholder="按ENTER键执行搜索" id="filter" />
                    </label>
                </div>
                <div style="margin-top: 15px" id="dwClassDiv"><span style="font-weight: bold">仓库分类：</span>
                    <span class="label" style="cursor: pointer;margin-right: 8px" onclick="themeChange(this)" th:each="item:${labels}" th:text="${item.na}" th:attr="cd=${item.cd}"></span>
                </div>
                <div id="themeItemDiv">
                    <div class="themeItem" style="margin-top: 12px;" th:each="label:${labels}" th:attr="cd=${label.cd}">
                        <span style="font-weight: bold;">类别明细：</span>
                        <span class="label" style="cursor: pointer;margin-right: 8px" onclick="themeItemChange(this)" th:each="child:${label.children}" th:text="${child.na}" th:attr="cd=${child.cd}"></span>
                    </div>
                </div>
                <form id="formId">
                    <input name="cdLabels" type="hidden" id="cdLabels">
                    <input name="na" type="hidden" id="na">
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="etl:dw:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="etl:dw:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="etl:dw:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="etl:dw:export">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
     <div class="modal inmodal fade" id="myModal5" tabindex="-1" role="dialog" aria-hidden="true">
         <div class="modal-dialog modal-lg">
             <div class="modal-content">
                 <input type="hidden" id="idBus">
                 <div class="modal-header">
                     <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
                     </button>
                     <h4 class="modal-title">表创建语句</h4>
                 </div>
                 <div class="modal-body">
                     <textarea id="creSql" style="width: 100%;" rows="10"></textarea>
                 </div>
                 <div class="modal-footer">
                     <button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
                     <button type="button" class="btn btn-primary" onclick="copyTableCreSql()">复制</button>
                     <button type="button" class="btn btn-danger" onclick="execSql()">执行</button>
                 </div>
             </div>
         </div>
     </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('etl:dw:edit')}]];
        var removeFlag = [[${@permission.hasPermi('etl:dw:remove')}]];
        var prefix = ctx + "base/etl/dw";
        var themeItemChange = function (ele) {
            var cd = $("#dwClassDiv").find("span.label-info:first").attr("cd");
            $("#themeItemDiv").find("div.themeItem").hide();
            $("#themeItemDiv").find("div.themeItem[cd='"+cd+"']").show();
            if(ele!=null) {
                if($(ele).hasClass("label-info")) {
                    $(ele).removeClass("label-info");
                } else {
                    $(ele).addClass("label-info");
                }
            }
            refreshTable();
        };
        function themeChange(ele) {
            $("#dwClassDiv").find("span.label").removeClass("label-info");
            $(ele).addClass("label-info");
            themeItemChange(null);
        }
        function refreshTable() {
            debugger;
            var cd = $("#dwClassDiv").find("span.label-info:first").attr("cd");
            var items = $("#themeItemDiv").find("div.themeItem[cd='"+cd+"'] span.label-info");
            var cds = "";
            //                         <span class="label" style="cursor: pointer;margin-right: 8px" onclick="themeItemChange(this)" th:each="child:${label.children}" th:text="${child.na}" th:attr="cd=${child.cd}"></span>
            $.each(items,function (index,item) {
               cds += $(this).attr("cd")+",";
            });
            $("#cdLabels").val(cd.length>0?cds.substring(0,cds.length-1):"");
            $("#na").val($("#filter").val());
            $.table.refresh();
        }
        function createDwTable(idDw) {
            $.ajax({
                url:prefix+"/generateCreateTableSql?idDw="+idDw,
                cache: false,
                type: 'GET',
                dataType: 'json',
                success: function (data) {
                    if(data.code=="0") {
                        $("#creSql").html(data.data);
                        $("#idBus").val(idDw);
                        $("#myModal5").modal("show");
                    } else {
                        $.modal.alertError(data.msg);
                    }
                }
            });
        }
        function showFieldDiff(idDw,title) {
            $.modal.open("["+title+"]表结构",prefix+"/table/flds/diff?idDw="+idDw);
        }
        function execSql() {
            var sql = $("#creSql").val();
            if(sql.indexOf("create table ")>-1) {
                $.ajax({
                    url: prefix+"/createTable",
                    type: 'POST',
                    dataType: 'json',
                    data: JSON.stringify({"sqlCreat":$("#creSql").val(),"idBus":$("#idBus").val()}),
                    contentType: 'application/json;charset=UTF-8',
                    success: function (result) {
                        $.table.refresh();
                        if(result.code==0) {
                            $.modal.alertSuccess(result.msg);
                        } else {
                            $.modal.alertError(result.msg);
                        }
                    }
                });
            } else {
                $.modal.alertWarning("请先生成建表语句");
            }
        }
        $(function() {
            $("#dwClassDiv").find("span.label:first").addClass("label-info");
            themeItemChange();
            var options = {
                url: prefix + "/list2",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "标准仓库",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'idDw',
                    title: '',
                    visible: false
                },
                {
                    field: 'cdTb',
                    title: '表名'
                },
                {
                    field: 'na',
                    title: '表描述'
                },
                {
                    field: 'fgTbCre',
                    title: '建表标志',
                    formatter: function(value, row, index) {
                        if(row.fgTbCre==1) {
                            return "已建表";
                        } else {
                            return "未创建";
                        }
                    }

                },
                {
                    field: 'euExecType',
                    title: '执行类型',
                    formatter: function(value, row, index) {
                        if(row.euExecType=="all") {
                            return "全量";
                        } else if(row.euExecType=="date") {
                            return "日期";
                        } else if(row.euExecType=="insert") {
                            return "仅新增";
                        } else {
                            return "未分类";
                        }
                    }
                },
                {
                    field: 'fgAct',
                    title: '启用标志'
                },
                {
                    field: 'dtSysCre',
                    title: '创建时间'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.idDw + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        debugger;
                        if(row.fgTbCre==0) {
                            actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="createDwTable(\'' + row.idDw + '\')"><i class="fa fa-table"></i>建表</a> ');
                        } else {
                            actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="showFieldDiff(\'' + row.idDw + '\',\''+row.cdTb+'\')"><i class="fa fa-table"></i>表结构</a> ');
                        }
                        actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.idDw + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
            $("#filter").bind('keyup',function (event) {
                if(event.keyCode=="13") {
                    refreshTable();
                }
            });
        });
    </script>
</body>
</html>